<!--
 * @Descripttion: damage报损报溢新增
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-17 20:26:23
 * @LastEditors: cxguo
 * @LastEditTime: 2020-07-07 16:19:58
 -->
<template>
  <div>
    <!-- userData: {{ userData }} -->
    <el-form ref="userData" :model="dataObj" :rules="rulesValidate" label-width="80px">
      <el-form-item label="昵称:" prop="username">
        <el-input v-model="dataObj.username" />
      </el-form-item>
      <el-form-item label="手机:" prop="phonenum">
        <el-input v-model="dataObj.phonenum" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input v-model="dataObj.password" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { oftenuse } from '@/libs/mixins'
import { register } from '@/api/sys/users'
import { isTelOrPhone } from '@/utils/validate'

export default {
  name: 'UserSave',
  mixins: [oftenuse],
  props: {
    userData: {
      type: Object,
      default: () => { return null }
    }
  },
  data() {
    return {
      rulesValidate: {
        username: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        phonenum: [
          { validator: isTelOrPhone, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.initData()
  },
  methods: {
    initData() {
      if (this.userData) this.dataObj = this.userData
    },
    // api
    saveData() {
      const userObj = this.dataObj
      return new Promise((resolve, reject) => {
        register(userObj).then(res => {
          if (!res.data.flag) {
            reject(res)
            return this.$notify.error({ title: '保存失败！', message: res.data.message || '' })
          } else {
            resolve(res)
            return this.$notify.success({ title: '保存成功！' })
          }
        }).catch(err => {
          reject(err)
          this.$notify.error({ title: '保存异常！', message: err || '' })
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
